<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" session="false" %>
<%@ page import="com.atozsoft.commonutil.Constants"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.text.SimpleDateFormat"%>
	
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- 
  - Author(s): mengbq
  - Date: 2014-11-14 15:20:29
  - Description:
-->
<head>
    <%@include file="/common.jsp"%>
	<%@include file="/common/common.jsp"%>
    <%@include file="/coframe/tools/skins/common.jsp" %>
	<title>流程节点按钮配置</title>
	<style type="text/css">
	    html, body{
	        margin:0;padding:0;border:0;width:100%;height:100%;overflow:hidden;
	    }    
    </style>
     <%
 	 	String processDefID = request.getParameter("processDefID");
 	 	
 	 	String pdstatus = "";
		if(request.getParameter("pdstatus")!=null){
			pdstatus = (String)request.getParameter("pdstatus");
		}
 	%>
</head>
<body>
<div style="padding:0px 0px 0px 0px;">
		<div class="nui-toolbar" style="border-bottom:0;">
        	<table style="width:100%;">
            	<tr>
		            <td style="width:100%;">
		                <a id="saveBtn" class="nui-button" style="width:60px;" iconCls="icon-save" onclick="onOk()">保存</a>
				        <a id="cancelBtn" class="nui-button" style="width:60px;" iconCls="icon-cancel" onclick="onCancel()">取消</a>
		            </td>
	            </tr>
	        </table>
    	</div>
    </div>
	<div class="nui-fit">
		<div id="datagrid1" class="mini-datagrid" style="width:100%;height:100%" 
       		 url="com.atozsoft.wf.processManager.queryProcessInfoButtonNode.biz.ext" idField="nodeid" dataField="list" showpager="false"
      	     allowCellEdit="true" allowCellSelect="true" multiSelect="false"
      		 allowCellValid="true">
	        <div property="columns">
	            <div type="indexcolumn" width="30px" headerAlign="center">序号</div>
	            <div field="configbuttonid" visible="false" width="120px" headerAlign="center" ></div>   
	            <div field="nodename"  width="120px" headerAlign="center">当前节点
	                <!--<input property="editor" class="mini-textbox" style="width:100%;" />-->
	            </div>                
	            <div field="buttontype" width="300px"  displayField="buttonName" headerAlign="center">按钮配置
	               <input property="editor" class="nui-buttonedit" onbuttonclick="onButtonEdit" style="width:99%;" allowInput="false"/>
	            </div>
	        </div>
	    </div>
    </div>



	<script type="text/javascript">
	    nui.parse();
	    var buttonObj = {}; //存放配置的按钮信息
	    var isLoad = true; //是否加载 drawcell事件
	    var processDefineId = "<%=processDefID %>"; //流程定义Id
    	var grid = nui.get("datagrid1");
    	
    	//页面操作控制
		var pdstatus = "<%=pdstatus %>";
		if(pdstatus!=null && pdstatus!="" && pdstatus=="stop")
		{
			nui.get("saveBtn").hide();
			nui.get("cancelBtn").hide();
		}
    	
    	grid.load({processDefineId:processDefineId,type:"nodeButton"});
    	grid.sortBy("configbuttonid","asc");
    	grid.on("drawcell", function (e) { //显示选中的按钮      每次选择按钮之后都会触发该事件
			   var record = e.record,
		           field = e.field,
		           row = e.row;
				if (record != null && isLoad) {
				   var bType = record.buttontype; //获取节点对应的按钮信息
				   bType = (bType == null)?  "" : bType;
				   buttonObj[row.configbuttonid] = bType; //将按钮信息和id放到对象中
				   if("buttontype" == field){  //"按钮配置" 赋值
				     if(bType != "" && bType.length > 4){
				         var bId = "";
	                     var bName = "";
					     var buttonArray = (bType.indexOf(",") > 0)?bType.split(","):bType; //将按钮信息转换为数组
					     for(var i=0,len =buttonArray.length;i<len;i++){ //获取按钮的Id和名称
					        var bData = buttonArray[i];
	                        var n = bData.indexOf("-");
	                        if(i == len -1){
	                          bId += bData.substring(0,n);
	                          bName += bData.substring(n+1);
	                        }else{
	                          bId += bData.substring(0,n) +",";
	                          bName += bData.substring(n+1) +",";
	                        }
					     }
				        e.cellHtml = bName; //赋值
				     }
				   }
			    }
			});
    	
    	
    	
    	
    	//保存
    	function onOk(){
    	   grid.validate();
            if (grid.isValid() == false) {
                return;
            }

            var dataMaps = grid.getData();
            var json = nui.encode({pButtonNotes:dataMaps,processDefineId:processDefineId});
           
           // grid.loading("保存中，请稍后......");
            $.ajax({
                url: "com.atozsoft.wf.processManager.editProcessInfoConfigButtonNode.biz.ext",
                data: json,
                type: "post",
                cache: false,
                contentType:'text/json',
                success: function (text) {
                    isLoad = true;
                     buttonObj = {};
                     nui.alert("设置成功!", "提示");
                    grid.reload();
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                }
            });
    	}
    	//取消
    	function onCancel(){
    	
    	}
    	//配置节点按钮
    	 function onButtonEdit(e) {
             nui.open({
                url: "<%= request.getContextPath() %>/wf/process/process_Info_NodeButtonList.jsp",
                title: "配置节点按钮",
                width: 650,
                height: 420,
                allowResize:false,
                onload: function () {
					var iframe = this.getIFrameEl();
					 var row = grid.getSelected();
               		 var data = nui.encode({buttonStr:buttonObj[row.configbuttonid]}); //将按钮信息转递
               		 iframe.contentWindow.SetData(data);
                },
                ondestroy: function (action) {
                    if (action == "ok") {
                          var iframe = this.getIFrameEl();
		                    var data = iframe.contentWindow.GetData(); //接收选中的按钮信息
		                    data = nui.clone(data);
		                    if (data) {
		                      var bId = "";
		                      var bName = "";
		                      for(var i=0,len=data.length;i<len;i++){ //获取选中的按钮id 和名称
		                        var bData = data[i];
		                        var n = bData.indexOf("-");
		                        if(i == len -1){
		                          bId += bData.substring(0,n);
		                          bName += bData.substring(n+1);
		                        }else{
		                          bId += bData.substring(0,n) +",";
		                          bName += bData.substring(n+1) +",";
		                        }
		                      }
		                        isLoad = false; //禁止加载drawcell事件
		                        grid.cancelEdit();
		                        var row = grid.getSelected();
		                        var bType = data.join(",");  //将选中的按钮信息转换为字符串
		                        buttonObj[row.configbuttonid] = bType; //更改节点对应的按钮信息
		                        grid.updateRow(row, {    //重新赋值
		                            buttontype: bType,
		                            buttonName: bName
		                        });
		                    }

                    }
                }
            });
        }
        
    </script>
</body>
</html>